package com.liujit.standard.basis.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.standard.basis.admin.domain.result.SysRoleMenuAuthResult;
import com.liujit.standard.basis.admin.domain.params.RoleMenuParam;
import com.liujit.standard.basis.admin.mapper.SysMenuMapper;
import com.liujit.standard.basis.admin.mapper.SysRoleMapper;
import com.liujit.standard.basis.admin.mapper.SysRoleMenuMapper;
import com.liujit.standard.basis.admin.mapper.SysUserRoleMapper;
import com.liujit.standard.basis.admin.mapper.*;
import com.liujit.standard.basis.admin.model.SysMenu;
import com.liujit.standard.basis.admin.model.SysRole;
import com.liujit.standard.basis.admin.model.SysRoleMenu;
import com.liujit.standard.basis.admin.service.SysRoleMenuService;
import com.liujit.standard.basis.admin.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description 系统角色Service实现类
 * @Author LiuJun
 * @Date 2021-03-26 09:00:34
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(String id) {
        List<String> ids = Arrays.asList(id.split(","));
        // 清除角色菜单
        for (String s : ids) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("role_id", Convert.toLong(s));
            sysRoleMenuMapper.delete(wrapper);
        }
        // 清除用户角色
        for (String s : ids) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("role_id", Convert.toLong(s));
            sysUserRoleMapper.delete(wrapper);
        }
        int res = sysRoleMapper.deleteBatchIds(ids);
        if (res > 0) {
            return true;
        }
        return false;
    }

    @Override
    public SysRoleMenuAuthResult getRoleMenu(Long id) {
        SysRoleMenuAuthResult result = new SysRoleMenuAuthResult();
        QueryWrapper wrapper = new QueryWrapper();
        List<SysMenu> list = sysMenuMapper.selectList(wrapper);
        TreeNodeConfig config = new TreeNodeConfig();
        config.setParentIdKey("pid");
        config.setParentIdKey("pid");
        config.setNameKey("title");
        config.setWeightKey("seq");
        List<Tree<Long>> menus = TreeUtil.build(list, 0L, config, (object, tree) -> {
            tree.putExtra("id", object.getId());
            tree.putExtra("pid", object.getPid());
            tree.putExtra("title", object.getTitle());
            tree.putExtra("checked", 0);
        });
        result.setMenus(menus);
        List<Long> roleMenus = sysRoleMapper.selectRoleByRoleId(id);
        result.setRoleMenus(roleMenus);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveRoleMenu(RoleMenuParam param) {
        Long roleId = param.getRoleId();
        // 根据角色id删除
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", param.getRoleId());
        sysRoleMenuMapper.delete(wrapper);
        List<Long> menuIds = param.getMenuIds();
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            list.add(sysRoleMenu);
        }
        return sysRoleMenuService.saveBatch(list);
    }

}
